package fr.isis.cloud;

import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Mail extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String objet = req.getParameter("objet");
		String message = req.getParameter("message");
		String addrUsr = req.getParameter("addrusr");

		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("julien.plagnes@gmail.com",
					"Julien & Co"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					addrUsr, "User"));
			msg.setSubject(objet);
			msg.setText(message);
			Transport.send(msg);

			resp.sendRedirect("/");
		} catch (AddressException e) {
			// ...
		} catch (MessagingException e) {
			// ...
		}
	}
}
